package com.hongdee.crm.base.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.Template.entity.TCrmTemplateRef;
import com.hongdee.crm.base.dao.ICarDao;
import com.hongdee.crm.base.entity.TCrmSurveyGroup;
import com.hongdee.crm.base.service.ITCrmBaseSurveryService;
import com.hongdee.crm.wo.dao.ITCrmWoXfDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/10.
 */
@Service
public class TCrmBassSurveryServiceImpl implements ITCrmBaseSurveryService {
    @Autowired
    private ICarDao iCarDao;
    private String tabName = "T_CRM_SURVERY_GROUP";
    @Autowired
    private IHibernateDao iHibernateDao;
    @Override
    public void update(String id, String status) throws Exception {
        String hql = "update TCrmSurveyGroup  set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.iCarDao.update(hql, params);
    }

    @Override

    public TCrmSurveyGroup saveOrUpdate(String uid,TCrmSurveyGroup tCrmSurveyGroup) throws Exception {
        String companyId = TokenUtil.getCompanyId(uid);

        //根据分类编号、分来名称去重
        if (null == tCrmSurveyGroup.getId() || StringUtils.isEmpty(tCrmSurveyGroup.getId())) {
            //新增时判断是否已经存在
            if (isExist(tCrmSurveyGroup.getGroupCode(),tCrmSurveyGroup.getGroupName(),0,null)){

                throw new Exception("分类编号或分类名称名称已存在");
            }
            tCrmSurveyGroup.setStatus("1");
            tCrmSurveyGroup.setCompanyId(companyId);
            this.iCarDao.saveOrUpdate(tCrmSurveyGroup);
        } else {
            if (isExist( tCrmSurveyGroup.getGroupCode(), tCrmSurveyGroup.getGroupName(),1, tCrmSurveyGroup.getId())) {
                throw new Exception("分类编号或分类名称名称已存在");
            }
            tCrmSurveyGroup.setCompanyId(companyId);
            this.iCarDao.saveOrUpdate(tCrmSurveyGroup);
        }
        //修正下一级编码
        return  tCrmSurveyGroup;
    }
    //判断分类编号，分类名称是否重复
    private boolean isExist(String groupCode,String groupName ,int type,String id) throws Exception {
        String sql = "select * from " + tabName + " where ( group_code= ? or group_name = ? ) ";
        List param = new ArrayList();
        param.add(groupCode);
        param.add(groupName);
        //分类编号和分类名称不可以重复
        if (1 == type) {
            sql += " and  id != ? ";
            param.add(id);
        }
        Map map = this.iCarDao.page(sql, param.toArray(), null, null);
        return Integer.valueOf(map.get("total").toString()) > 0;
    }
    /*
    * 根据条件查询
    * */
    @Override
    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
        return this.iCarDao.info(sql, id);
    }
    /*
    * 分页
    * **/
    @Override
    public Map newPage(String start, String limit, String groupCode) throws Exception{
        String sql = "select * from T_CRM_SURVERY_GROUP   ";
        List<Object> params = new ArrayList();
        if (!StringUtils.isEmpty(groupCode)) {
            sql += " where group_code like ? or  group_name like ? ";
            params.add("%" + groupCode + "%");
            params.add("%" + groupCode + "%");
        }
        sql+="order by status desc,disp_order asc";
        return this.iCarDao.page(sql,params.toArray(),start,limit);
    }
}
